unit uCapResForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uBaseCalcForm, StdCtrls, ExtCtrls;

type
  TfmCapRes = class(TfmBaseCalcForm)
    edCap: TLabeledEdit;
    edP1: TLabeledEdit;
    edResult: TLabeledEdit;
    cbCapMult: TComboBox;
    laResult: TLabel;
    cbFreqMult: TComboBox;
    rgElement: TRadioGroup;
    rgCalc: TRadioGroup;
    Bevel1: TBevel;
    laCap: TLabel;
    procedure edP1Change(Sender: TObject);
    procedure edP1KeyPress(Sender: TObject; var Key: Char);
    procedure rgElementClick(Sender: TObject);
  private
    { Private declarations }
    function CalcXRes(ACap: double; AFreq: single): single;
    function CalcVal(AXRes, AFreq: single): double;
  protected
    function DoCheckFields: boolean; override;
    procedure FieldsChanged; override;
  public
    { Public declarations }
    class function CalcFriendlyName: string; override;
    class function RunKey: string; override;
    class procedure InvokeCalc; override;

    procedure AfterConstruction; override;
  end;

var
  fmCapRes: TfmCapRes;

implementation

{$R *.dfm}

uses uFormReg, uLocalization, uENoms;

const
  strCapMult: array[0..2] of string = ('pf','nf','uf');
  sfCapMult: array[0..2] of single = (1.0e-12,1.0e-9,1.0e-6);

  sfFreqMult: array[0..2] of single = (1.0, 1.0e3, 1.0e6);

  sfIndMult: array[0..2] of single = (1.0e-9,1.0e-6,1.0e-3);
  strIndMult: array[0..2] of string = ('nH','uH','mH');

{ TfmCapRes }

procedure TfmCapRes.AfterConstruction;
begin
  inherited;
  LocalizeStrings(cbFreqMult.Items, false);
  LocalizeStrings(rgElement.Items, false);
  LocalizeStrings(rgCalc.Items);
  rgElement.Caption := GetString('#global\element');
  rgCalc.Caption := GetString('#global\calctype');
  rgElementClick(nil);
  cbFreqMult.ItemIndex := 0;
end;

class function TfmCapRes.CalcFriendlyName: string;
begin
  Result := 'Reactive resistance';
end;

function TfmCapRes.CalcXRes(ACap: double; AFreq: single): single;
begin
  Result := 1.0/(2.0*PI*AFreq*ACap);
end;

function TfmCapRes.DoCheckFields: boolean;
begin
  Result := checkFieldMore(edCap, 0) and checkFieldMore(edP1, 0);
  if not Result then edResult.Text := '';
end;

procedure TfmCapRes.FieldsChanged;
var tmp,vmult: single;
begin
  inherited;

  case rgCalc.ItemIndex of
    0: begin
        if rgElement.ItemIndex = 0 then vmult := sfCapMult[cbCapMult.ItemIndex]
        else vmult := sfIndMult[cbCapMult.ItemIndex];
        tmp := CalcXRes(StrToFloat(edCap.Text)*vmult, StrToFloat(edP1.Text)*sfFreqMult[cbFreqMult.ItemIndex]);
        edResult.Text := formatValue(tmp);
      end;
    1: begin
        tmp := CalcVal(StrToFloat(edCap.Text), StrToFloat(edP1.Text)*sfFreqMult[cbFreqMult.ItemIndex]);
        case rgElement.ItemIndex of
          0: edResult.Text := formatValueC(tmp);
          1: edResult.Text := formatValueL(tmp);
        end;
    end;
  end;
end;

class procedure TfmCapRes.InvokeCalc;
begin
  Application.CreateForm(TfmCapRes, fmCapRes);
  inherited;
end;

class function TfmCapRes.RunKey: string;
begin
  Result := 'xres';
end;

procedure TfmCapRes.edP1Change(Sender: TObject);
begin
  inherited;
  DoFieldChanged;
end;

procedure TfmCapRes.edP1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', chr(VK_BACK), DecimalSeparator, chr(VK_DELETE)]) then Key := #0;
end;

procedure TfmCapRes.rgElementClick(Sender: TObject);
var i: integer;
begin
  inherited;

  case rgCalc.ItemIndex of
    0: begin
        SetLabels('P1', GetString('#global\frequency'), '');
        SetLabels('Result', GetString('#global\resistance'), GetString('#global\ohm'));
        cbCapMult.Visible := true;

        case rgElement.ItemIndex of
          0: begin
              SetLabels('Cap', GetString('#global\capacitance'), '');
              cbCapMult.Clear;
              for i := low(strCapMult) to High(strCapMult) do
                cbCapMult.AddItem(GetString('#global\'+strCapMult[i]), nil);
              cbCapMult.ItemIndex := 2;
            end;
          1: begin
              SetLabels('Cap', GetString('#global\inductance'), '');
              cbCapMult.Clear;
              for i := low(strIndMult) to High(strIndMult) do
                cbCapMult.AddItem(GetString('#global\'+strIndMult[i]), nil);
              cbCapMult.ItemIndex := 1;
            end;
        end;
      end;
    1: begin
        SetLabels('Cap', GetString('#global\resistance'), GetString('#global\ohm'));
        SetLabels('P1', GetString('#global\frequency'), '');
        cbCapMult.Visible := false;

        case rgElement.ItemIndex of
          0: begin
              SetLabels('Result', GetString('#global\capacitance'), '');
            end;
          1: begin
              SetLabels('Result', GetString('#global\inductance'), '');
            end;
        end;
      end;
  end;
  laCap.Visible := not cbCapMult.Visible;
end;

function TfmCapRes.CalcVal(AXRes, AFreq: single): double;
begin
  Result := 1.0/(2*PI*AFreq*AXRes);
end;

initialization
  RegisterCalc(TfmCapRes);

end.
